<?php

class ImapWrapperEmails
{

    private $connection;

    public function __construct($connection) {
        $this->connection = $connection;
    }

    public function getNewMail() {
        $emails = imap_search($this->connection, 'NEW');
        return $this->getEmails($emails);
    }

    public function getAllMail() {
        $emails = imap_search($this->connection, 'ALL');
        return $this->getEmails($emails);
    }

    public function getEmailByUid($uid) {
        $emailnr = @imap_msgno($this->connection, $uid);
        return $emailnr == 0 ? false : new ImapWrapperEmail($this->connection, $emailnr);
    }

    private function getEmails($emails) {
        $wrappers = array();
        if (!empty($emails)) {
            foreach ($emails as $emailnr) {
                $wrappers[] = new ImapWrapperEmail($this->connection, $emailnr);
            }
        }
        return $wrappers;
    }

}